package xyz.myachin.downloader.ui.share

import android.app.Activity
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import xyz.myachin.downloader.dm.Downloader

class ProxySharingActivity : Activity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        if (intent.action != Intent.ACTION_SEND
            || intent.type != "text/plain"
            || (intent.getStringExtra(Intent.EXTRA_TEXT)?.startsWith("http") != true)
        ) {
            finish()
            return
        }

        handleIntent(intent)
    }

    private fun handleIntent(intent: Intent) {
        val req = Downloader.prepareDownloadTask(Uri.parse(intent.getStringExtra(Intent.EXTRA_TEXT)))
        Downloader.enqueue(req, SharingType.PROXY)
        finish()
    }
}